<template>
  <div class="caddaddress">
    <van-nav-bar title="添加配送地址" left-arrow @click-left="onClickLeft" />
    <van-address-edit
      :area-list="areaList"
      show-set-default
      show-search-result
      :address-info="info"
      save-button-text="确定"
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @change-detail="onChangeDetail"
    />
  </div>
</template>
<script>
import areaList from "@/../mock/area";
import { Dialog } from "vant";
import address_search_list from "@/../mock/address_search_list";
export default {
  data() {
    return {
      info: {
        isDefault: true,
        name: "",
        tel: '',
        // addressDetail: "麓谷企业广场 A2栋3单元 306",
        id:0
      },
      areaList,
      searchResult: []
    };
  },
  created() {
     this.getusernametel();
    // console.log(this.$store.state.address_id); //id还没写
    // console.log(JSON.parse(localStorage.getItem("address_data_list") || "[]"));
    this.id=Number(this.$route.params.id);
  },
  methods: {
    getusernametel() {
      const user = JSON.parse(localStorage.getItem("user") || "[]");
      this.info.name = user.uname;
      this.info.tel = user.username;
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    onSave(data_info) {
      //初始化id
      this.$store.commit("address_id_add");
      // 获取id
      var id = this.$store.state.address_id;
      // 把id保存到本地
      localStorage.setItem("address_id", id);
      // 添加id到信息里
      data_info.id = id;
      data_info.address = data_info.addressDetail;
      // 是第一条时

      if (data_info.id === 1) {
        if (data_info.isDefault == false) {
          Dialog.alert({
            message: "已自动将当前地址设为默认地址"
          }).then(() => {
            // on close
            this.$router.push("/mine/checkaddress/"+this.id);
          });
        }
        data_info.isDefault = true;
      } else {
        // console.log(2);
        // 不是第一条
        if (data_info.isDefault == true) {
          // 改变默认地址为false
          this.$store.commit("changedaddress");
        }
      }
      this.$Toast("保存成功");
      // console.log(data_info);
      // 保存信息
      this.$store.commit("sava_address_data", data_info);
      // 确定是否是默认地址

      // 跳转
      // console.log(data_info)
      this.$router.push("/mine/checkaddress/"+this.id);
    },
    onChangeDetail(val) {
      this.searchResult = [];
      const search_list = address_search_list.search_list;
      if (val) {
        // 内部的数据
        // console.log(address_search_list)
        // 拿到数据
        // console.log(search_list)
        search_list.forEach((el, index) => {
          if (el.name.indexOf(val) != -1) {
            // console.log(el.name).push
            el.address=""
            el.name+=el.short_address
            console.log(el)
            this.searchResult.push(el);
          }
        });
      } else {
        this.searchResult = [];
      }
    }
  }
};
</script>
<style lang="less" scoped>
.caddaddress {
  .van-nav-bar {
    background-image: linear-gradient(90deg, #0af, #0085ff);
    .van-icon {
      color: #fff;
      font-size: 18px;
    }
    .van-nav-bar__title {
      font-weight: bold;
      color: #fff;
      font-size: 18px;
    }
  }
  .van-address-edit[data-v-28589d54] {
    padding: 0px;
    /deep/.van-button {
      border-radius: 0px;
      background-color: #00d762;
      border: 0px;
    }
  }
}
</style>